package com.ruoyi.system.service.impl;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

import com.google.zxing.BarcodeFormat;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.QRCode;
import com.ruoyi.common.utils.QRCodeUtil;
import com.ruoyi.common.utils.equipment.Demo;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.system.domain.TourismAppointment;
import com.ruoyi.system.domain.TourismRecord;
import com.ruoyi.system.domain.TourismStatistics;
import com.ruoyi.system.mapper.TourismAppointmentMapper;
import com.ruoyi.system.mapper.TourismRecordMapper;
import com.ruoyi.system.mapper.TourismStatisticsMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.IOUtils;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.TourismPersonnelMapper;
import com.ruoyi.system.domain.TourismPersonnel;
import com.ruoyi.system.service.ITourismPersonnelService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;

/**
 * 预约人员明细Service业务层处理
 *
 * @author gqd
 * @date 2020-12-25
 */
@Service
@Slf4j
@Transactional
public class TourismPersonnelServiceImpl implements ITourismPersonnelService
{
    @Resource
    private TourismPersonnelMapper tourismPersonnelMapper;
    @Resource
    private TourismRecordMapper tourismRecordMapper;
    @Resource
    private TourismStatisticsMapper tourismStatisticsMapper;
    @Resource
    private TourismAppointmentMapper tourismAppointmentMapper;

    /**
     * 查询预约人员明细
     *
     * @param id 预约人员明细ID
     * @return 预约人员明细
     */
    @Override
    public TourismPersonnel selectTourismPersonnelById(Long id)
    {
        return tourismPersonnelMapper.selectTourismPersonnelById(id);
    }

    /**
     * 查询预约人员明细列表
     *
     * @param tourismPersonnel 预约人员明细
     * @return 预约人员明细
     */
    @Override
    public List<TourismPersonnel> selectTourismPersonnelList(TourismPersonnel tourismPersonnel)
    {
        return tourismPersonnelMapper.selectTourismPersonnelList(tourismPersonnel);
    }

    /**
     * 新增预约人员明细
     *
     * @param tourismPersonnel 预约人员明细
     * @return 结果
     */
    @Override
    public int insertTourismPersonnel(TourismPersonnel tourismPersonnel)
    {
        tourismPersonnel.setCreateTime(DateUtils.getNowDate());
        return tourismPersonnelMapper.insertTourismPersonnel(tourismPersonnel);
    }

    /**
     * 修改预约人员明细
     *
     * @param tourismPersonnel 预约人员明细
     * @return 结果
     */
    @Override
    public int updateTourismPersonnel(TourismPersonnel tourismPersonnel)
    {
        return tourismPersonnelMapper.updateTourismPersonnel(tourismPersonnel);
    }

    /**
     * 批量删除预约人员明细
     *
     * @param ids 需要删除的预约人员明细ID
     * @return 结果
     */
    @Override
    public int deleteTourismPersonnelByIds(Long[] ids)
    {
        return tourismPersonnelMapper.deleteTourismPersonnelByIds(ids);
    }

    /**
     * 删除预约人员明细信息
     *
     * @param id 预约人员明细ID
     * @return 结果
     */
    @Override
    public int deleteTourismPersonnelById(Long id)
    {
        return tourismPersonnelMapper.deleteTourismPersonnelById(id);
    }

    @Override
    public AjaxResult insertPerson(Integer userId, List<TourismPersonnel> list, Integer appointmentId, String date, Integer personNum) {
        try {
            //预约信息配置表
            TourismAppointment tourismAppointment = tourismAppointmentMapper.selectById(appointmentId);
            //获取处理预约时间
            SimpleDateFormat  mFormat = new SimpleDateFormat("yyyy-MM-dd:HH:mm:ss");
            String aa= tourismAppointment.getEndTimes();
            Date date1 = mFormat.parse(date+":"+aa+":00");
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date1);
            calendar.set(Calendar.DAY_OF_MONTH,calendar.get(Calendar.DAY_OF_MONTH)+1);
            log.info("预约用户id"+userId+"预约时间"+mFormat.format(calendar.getTimeInMillis()));
            int codes = new Date().hashCode();
            String code= Demo.getQrCode(calendar,codes);
            log.info("二维码内容"+code+"编号"+codes);
            //生成二维码
            TourismRecord tourismRecord = new TourismRecord();
            tourismRecord.setUserId(userId);
            QRCode test = new QRCode();
            //二维码名字
            long Path = new Date().getTime();
            String filePath = RuoYiConfig.getUploadPath()+"/"+Path+".png";
            log.info(filePath+"=============================");
//            File file = new File(filePath+"/upload/"+Path+".png");
//            test.encode(code, file, BarcodeFormat.QR_CODE, 500, 500, null);
            QRCodeUtil.encodeQRCode(code,filePath);
            tourismRecord.setQrCode("https://wubu.xzrenli.cn"+"/upload/"+Path+".png");
            tourismRecord.setAppointmentId(appointmentId);
            SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
            tourismRecord.setCreateTime(sf.parse(date));
            int i = tourismRecordMapper.insertTourismRecord(tourismRecord);
            if(i <= 0){
                return AjaxResult.error("预约失败！");
            }
            System.out.println("+++++++++>" + tourismRecord.getId());
            //查询数据统计表
            TourismStatistics statistics = tourismStatisticsMapper.selectByAppIdAndDate(appointmentId, date);

            if(statistics !=null){
                //预约人数
                System.out.println(personNum+"预约人数");
                System.out.println("======>" + statistics.getPeopleSums() + personNum);
                //剩余人数
                System.out.println("------->" + tourismAppointment.getPeopleNumber());
                statistics.setPeopleSums(statistics.getPeopleSums() + personNum);
                statistics.setSurplusSums(tourismAppointment.getPeopleNumber() - (statistics.getPeopleSums() + personNum));
                tourismStatisticsMapper.updateTourismStatistics(statistics);
            }else{
                TourismStatistics tourismStatistics = new TourismStatistics();
                tourismStatistics.setAppointId(appointmentId);
                tourismStatistics.setStateTimes(date);
                tourismStatistics.setPeopleSums(personNum);
                tourismStatistics.setSurplusSums(tourismAppointment.getPeopleNumber() - personNum);
                tourismStatisticsMapper.insertTourismStatistics(tourismStatistics);
            }
            for(TourismPersonnel tourismPersonnel : list){
                if(!"".equals(tourismPersonnel.getRecordName())){
                    tourismPersonnel.setRecordId(tourismRecord.getId());
                    tourismPersonnelMapper.insertTourismPersonnel(tourismPersonnel);
                }
            }
            return AjaxResult.success("预约成功！");
        } catch (Exception  e) {
            e.printStackTrace();
            return AjaxResult.error(202,"异常");
        }


    }



}
